package com.example.demo;

import com.example.demo.bean.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 1，DispatcherServlet 分发请求
 * 2，不会再走视图处理器，而是直接将数据写入到输出流
 */
//@RestController
public class TestController {

    /**
     * @Autowired 用在JavaBean中的注解，通过byType形式，用来给指定的字段或方法注入所需的外部资源
     */
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "/index")
    public String testIndex(){
        return "hello world";
    }

    @RequestMapping("/getDB")
    public List<Map<String,Object>> testConnectDB(){
        String sql = "select * from test_table";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        /*for (Map<String,Object> map:list){
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            if(entries != null){
                Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
                while (iterator != null){
                    Map.Entry<String,Object> entry=(Map.Entry<String, Object>) iterator.next( );
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    System.out.println(key+":"+value);
                }
            }
        }*/

        return list;
    }

    @Resource
    private UserService userService;

    /*@RequestMapping("/getUser")
    @ResponseBody
    public User getUserById(HttpServletRequest request, Model model){

        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println("------------------"+id);
        User user = this.userService.getUserById(id);
        return user;
    }*/


}
